﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Product;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.Orm.View;
using OpenSoftStore.Orm.Model;

namespace OpenSoftStore.Client.UIL.ManageProduct
{
    public partial class ProductTransfer : UserControl
    {
        private const string _ShopId = "b0def1a7-bf25-45f2-b65a-6dc586dbfc2c";
        private const string _ProviderId = "46872df4-47b1-4875-bdbb-1f8f8d1023ee";
        private T_StoreIO storeIO;
        private T_StoreIOItem storeItem;
        private ProductLogic productBll;
        private AdminLogic adminBll;
        private ComboLogic comboBll;
        private List<V_StoreIOItem> storeItems;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public ProductTransfer()
        {
            productBll = new ProductLogic();
            adminBll = new AdminLogic();
            comboBll = new ComboLogic();
            storeItems = new List<V_StoreIOItem>();
            storeIO = new T_StoreIO();
            storeItem = new T_StoreIOItem();
            InitializeComponent();

            LoadControlData();
        }
        private void ProductTransfer_Load(object sender, EventArgs e)
        {
            txtProductSearch.Focus();
            txtProductSearch.Select();
        }
        private void LoadControlData()
        {
            var posDataSource = comboBll.GetComboByBuzType("Pos", _ShopId).ToList();

            cmbAdmin.ValueMember = "Id";
            cmbAdmin.DisplayMember = "Name";
            cmbAdmin.DataSource = adminBll.GetAdminList();

            colPosIn.DisplayMember = "DisplayText";
            colPosIn.ValueMember = "Value1";
            colPosIn.DataSource = posDataSource;

            colPosOut.DisplayMember = "DisplayText";
            colPosOut.ValueMember = "Value1";
            colPosOut.DataSource = posDataSource;

            dgvProductTransfer.DefaultCellStyle.NullValue = posDataSource[0].DisplayText;

            txtOrder.Text = productBll.GetCountNo("T_StoreIO", true);

            dgvProductTransfer.AutoGenerateColumns = false;
            dgvProduct.AutoGenerateColumns = false;
            dgvProduct.DataSource = productBll.GetProductList();
            lblProductCount.Text = dgvProduct.RowCount.ToString();

            SetDataGridStyle(dgvProduct);
            SetDataGridStyle(dgvProductTransfer);
        }

        public void SetDataGridStyle(DataGridView dgv)
        {
            dgv.ColumnHeadersHeight = 40;
            dgv.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(242, 242, 242);
            dgv.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.FromArgb(70, 141, 75);
            dgv.RowTemplate.DefaultCellStyle.SelectionForeColor = Color.FromArgb(255, 255, 255);
            dgv.RowTemplate.Height = 40;
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            if (dgvProduct.Rows.Count == 0)
            {
                FormSysMessage.ShowMessage("目前没有添加一个商品，请添加完商品再出库！");
                return;
            }

            if (dgvProduct.SelectedRows[0] != null)
            {
                string id = dgvProduct.SelectedRows[0].Cells["productId"].Value.ToString();
                string name = dgvProduct.SelectedRows[0].Cells["name"].Value.ToString();
                decimal inPrice = Convert.ToDecimal(dgvProduct.SelectedRows[0].Cells["inPrice"].Value);
                string unit = dgvProduct.SelectedRows[0].Cells["unit"].Value.ToString();
                string pos = ((T_Combo)colPosIn.Items[0]).Value1;
                string pos2 = ((T_Combo)colPosOut.Items[0]).Value1;
                decimal subAmount = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["SubAmount"].Value);
                storeItems.Add(new V_StoreIOItem
                {
                    ProductId = id,
                    Name = name,
                    Price = inPrice,
                    Amount = 1,
                    Unit = unit,
                    Pos = pos,
                    Pos2 = pos2,
                    SubAmount = subAmount
                });

                dgvProductTransfer.DataSource = null;
                dgvProductTransfer.DataSource = storeItems;
                lblCount.Text = dgvProductTransfer.RowCount.ToString();

            }
        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            if (dgvProductTransfer.CurrentRow != null)
            {
                var row = dgvProductTransfer.SelectedRows[0];
                string id = row.Cells["colId"].Value.ToString();
                var storeItem = storeItems.Where<V_StoreIOItem>(s => s.ProductId == id).FirstOrDefault();
                storeItems.Remove(storeItem);
                dgvProductTransfer.DataSource = null;
                dgvProductTransfer.DataSource = storeItems;
                lblCount.Text = dgvProductTransfer.RowCount.ToString();
            }
        }

        private void txtSearch_KeyUp(object sender, KeyEventArgs e)
        {
            string whereStr = txtProductSearch.Text;
            if (rbSelect.Checked)
            {

                dgvProduct.DataSource = productBll.GetProductList(string.Format(" where P.Name like '{0}%' or P.ProductNo like '{0}%' or P.Model like '{0}%' or prov.Name like '{0}%' or T.Name like '{0}%'", whereStr));
            }
            else
            {
                dgvProduct.DataSource = productBll.GetProdutView(" where E.BarCode='" + whereStr + "'");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (dgvProductTransfer.Rows.Count == 0)
            {
                FormSysMessage.ShowMessage("请选择商品！");
                return;
            }

            storeItems = (List<V_StoreIOItem>)dgvProductTransfer.DataSource;
            T_StoreIO storeIn = new T_StoreIO();
            T_StoreIO storeOut = new T_StoreIO();

            storeIn.Id = Guid.NewGuid().ToString("D");
            storeOut.Id = Guid.NewGuid().ToString("D");
            storeIn.Type = "入库";
            storeOut.Type = "出库";
            storeIn.BuzType = "仓库调入";
            storeOut.BuzType = "仓库调出";
            storeIn.StroeIONo = txtOrder.Text;
            storeOut.StroeIONo = txtOrder.Text;
            storeIn.AdminId = cmbAdmin.SelectedValue.ToString();
            storeOut.AdminId = cmbAdmin.SelectedValue.ToString();
            storeIn.AddTime = dtpTransfer.Value;
            storeOut.AddTime = dtpTransfer.Value;
            storeIn.Remark = txtInRemark.Text;
            storeOut.Remark = txtOutRemark.Text;
            storeIn.ProductKinds = dgvProductTransfer.Rows.Count;
            storeOut.ProductKinds = dgvProductTransfer.Rows.Count;
            storeIn.ShopId = _ShopId;
            storeOut.ShopId = _ShopId;
            storeIn.BuzUserId = _ProviderId;
            storeOut.BuzUserId = _ProviderId;

            productBll.AddStoreIO(storeIn, storeOut, storeItems);
            this.FindForm().Close();

        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.FindForm().Close();
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            this.FindForm().Close();
        }

        private void txtProductSearch_KeyUp(object sender, KeyEventArgs e)
        {
            string whereStr = txtProductSearch.Text;
            if (rbSelect.Checked)
            {
                dgvProduct.DataSource = productBll.GetProductList(string.Format(" where P.Name like '{0}%' or P.ProductNo like '{0}%' or P.Model like '{0}%' or prov.Name like '{0}%' or T.Name like '{0}%'", whereStr));
            }
            else
            {
                dgvProduct.DataSource = productBll.GetProductList(" where E.BarCode='" + whereStr + "'");
            }
            lblProductCount.Text = dgvProduct.RowCount.ToString();
        }

        private void txtProductSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (rbBarCode.Checked)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    string whereStr = txtProductSearch.Text;
                    dgvProduct.DataSource = productBll.GetProductList(" where E.BarCode='" + whereStr + "'");
                    lblProductCount.Text = dgvProduct.RowCount.ToString();
                    btnAddItem_Click(null, null);
                }
            }
        }

        private void rbBarCode_CheckedChanged(object sender, EventArgs e)
        {
            //if (rbBarCode.Checked)
            //{
            //    txtProductSearch.Text = "";
            //}
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }


    }
}
